<template>
  <el-dialog
    title="新增"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="closeDialog"
    width="80%"
    top="8vh"
  >
    <el-row>
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="130px"
      >
        <div class="rwfabg">
          <el-row>
            <el-col :span="24">
              <el-col :span="8">
                <el-form-item label="发起名称" prop="faqiName">
                  <el-input
                    v-model="dataForm.faqiName"
                    placeholder="发起名称"
                    readonly
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="16">
                <el-form-item label="任务有效期" required>
                  <el-col :span="11">
                    <el-date-picker
                      v-model="dataForm.rwYxqq"
                      type="date"
                      value-format="yyyy-MM-dd"
                      prop="rwYxqq"
                      placeholder="开始日期"
                      style="width: 100%"
                      readonly
                    ></el-date-picker>
                  </el-col>
                  <el-col :span="2">
                    <div class="text-center">至</div>
                  </el-col>
                  <el-col :span="11">
                    <el-date-picker
                      v-model="dataForm.rwYxqz"
                      type="date"
                      value-format="yyyy-MM-dd"
                      prop="rwYxqz"
                      placeholder="结束日期"
                      style="width: 100%"
                      readonly
                    ></el-date-picker>
                  </el-col>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="8">
                <el-form-item label="填报结束日期" prop="signDate">
                  <el-date-picker
                    v-model="dataForm.signDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="填报结束日期"
                    style="width: 100%"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="模板名称" prop="templateId">
                  <el-select
                    v-model="dataForm.templateId"
                    placeholder="模板名称"
                    class="xiaLa"
                    style="width: 107%"
                    :clearable="true"
                  >
                    <el-option
                      v-for="(item, index) in categoryList"
                      :key="index"
                      :label="item.templateName"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="发起类型" prop="userLx">
                  <el-select
                    v-model="dataForm.userLx"
                    placeholder="发起类型"
                    class="xiaLa"
                    style="width: 100%"
                    :clearable="true"
                    @change="changeValue"
                  >
                    <el-option
                      v-for="(item, index) in dataForm.zdrwTypeList"
                      :key="index"
                      :label="item.name"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col>
              <el-col :span="8">
                <el-button
                  type="text"
                  style="margin-left: 30px"
                  icon="el-icon-user-solid"
                  @click="addUser('bkhr')"
                ></el-button>
                <div style="margin-left: 100px; margin-top: -35px">
                  被考核人：
                  <span v-for="(item, index) in shouyuerendataList" :key="index"
                    >{{ item.userName }}、</span
                  >
                </div>
              </el-col>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-row>
    <renwuFqForm ref="renwuFqRef" :renwuFqForm="dataForm"></renwuFqForm>
    <!--  选择人员信息-->
    <UserCommonSelect ref="usercomment" @submitfy="submitfy"></UserCommonSelect>
    <span slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        plain
        @click="saveFormData"
        v-loading.fullscreen.lock="fullscreenLoading"
        >保存</el-button
      >
      <el-button type="primary" plain @click="visible = false">关闭</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {saveRenwuFaQi,isExistData} from "@/api/hetongrenwu/zhongdian/renwuchuangjian";

import UserCommonSelect from "@/views/hetongrenwu/components/usercomment-select";
import renwuFqForm from "@/views/hetongrenwu/zhongdianrenwu/zhongdianrwcj/rwfqnew/renwufq-form.vue";
import { effective } from "@/api/hetongrenwu/hetongyeji/hetongnd";

export default {
  components: {
    UserCommonSelect,
    renwuFqForm,
  },
  props: {
    addDataForm: {
    },
  },
  data() {
    var checkDate = (rule, value, callback) => {
      if (value) {
        if (
          new Date(value.replace(/-/g, "/")) <
          new Date(this.dataForm.rwYxqq.replace(/-/g, "/"))
        ) {
          callback("结束日期不能小于开始日期!");
        } else {
          callback();
        }
      } else {
        callback("请选择结束日期");
      }
    };
    var checkDateKai = (rule, value, callback) => {
      if (value) {
        if (
          new Date(value.replace(/-/g, "/")) >
          new Date(this.dataForm.rwYxqz.replace(/-/g, "/"))
        ) {
          callback("开始日期不能大于结束日期!");
        } else {
          callback();
        }
      } else {
        callback("请选择开始日期");
      }
    };
    return {
      visible: false,
      categoryList: [],
      dataForm: {},
      jsonData: "",
      shouyuerenNameList: "",
      renwuFqForm: {},
      shouyuerendataList: [],
      dataRule: {
        faqiName: [
          { required: true, message: "请输入发起名称", trigger: "change" },
        ],
        rwYxqq: [{ validator: checkDateKai, trigger: "change" }],
        rwYxqz: [{ validator: checkDate, trigger: "change" }],
        signDate: [
          { required: true, message: "请选择填报结束日期", trigger: "change" },
        ],
        templateId: [
          { required: true, message: "请选择模板名称", trigger: "change" },
        ],
        userLx:[
           { required: true, message: "请选择发起类型", trigger: "change" },
        ]
      },
      fullscreenLoading: false,
      zdrwBkhUsers: [],
    };
  },
  watch: {},
  created() {
    this.fneffective();
  },
  methods: {
    //关闭清空任务数据
    closeDialog() {
      this.$refs.renwuFqRef.dataList = [];
    },
    //初始化
    csh() {
      this.dataForm = this.addDataForm
      this.shouyuerendataList = [];
      this.shouyuerenNameList = "";
      // this.getRenWuFaQi();
    },
    init(data) {
      this.csh();
      this.visible = true;
      this.shouyuerendataList = [];
      if (data != undefined) {
        this.dataForm = data;
        this.shouyuerendataList = JSON.parse(data.shouyuejson);
        this.$forceUpdate();
      }
    },
    changeValue(value){
      if(value == 1){
        this.$refs.renwuFqRef.dataList = [];
      }
    },
    saveFormData() {
      //整理被考核人信息
      this.dataForm.zdrwBkhUsers = this.zdrwBkhUsers;
      this.dataForm.zdrwRenwufaqiDetails = this.$refs.renwuFqRef.dataList;
      if (this.shouyuerendataList == 0) {
        this.$message.warning("请选择被考核人");
        return;
      }
      var ids = ""
      this.dataForm.zdrwBkhUsers.forEach(ele=>{
        ids = ids + ele.userId + ","
      })
      var data = this.dataForm.zdrwRenwufaqiDetails;
      for(var i=0;i<data.length;i++){
        if(data[i].khrId == ''){
          this.$message.warning('第'+[i+1]+'行请选择考核人')
          return
        }
      }
      var params = {
        userIds:ids,
        userLx:this.dataForm.userLx,
        faqiName:this.dataForm.faqiName
      }
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          isExistData(params).then(res=>{
            if(res.data.data.data == ''){
              this.fullscreenLoading = true;
              saveRenwuFaQi(this.dataForm).then((res) => {
                this.$message.closeAll();
                this.$message.success("保存成功");
                this.visible = false;
                this.$emit("refreshDataList");
                this.fullscreenLoading = false;
              });
            }
            else{
              this.$message.warning('在本周期下该用户存在重点任务，请在'+res.data.data.data+'的重点任务中进行任务添加。')
            }
          })
          
        } else {
          return false;
        }
      });
    },
    addUser(type) {
      this.jsonData = JSON.stringify(this.shouyuerendataList);
      this.$nextTick(() => {
        this.$refs.usercomment.init("12", type, this.jsonData, "htcj");
      });
    },
    submitfy(data) {
      this.shouyuerendataList = [];
      this.zdrwBkhUsers = [];
      this.shouyuerendataList = data.tableDataUser;
      this.shouyuerendataList.forEach((ele) => {
        var user = {};
        user.userId = ele.userId;
        user.userName = ele.userCode;
        user.ryName = ele.userName;
        user.deptId = ele.deptId;
        user.userDeptName = ele.deptName;
        user.userZw = ele.userZw;
        user.userLx = "总部发起";
        user.userZj = ele.userZj;
        this.zdrwBkhUsers.push(user);
      });
    },
    //打开指标维护
    fnOpenZhibiao() {
      var param = {
        openZhi: true,
      };
      this.$emit("openZhibiao", param);
    },
    // 获取下拉
    fneffective() {
      effective({
        tagName: "重点任务",
      }).then((res) => {
        this.categoryList = res.data.data;
      });
    },
  },
};
</script>
<style scoped>
.rwfabg {
  border-radius: 16px;
  -webkit-box-shadow: 5px 5px 10px #e3e3ea, -5px -5px 10px #ffffff;
  box-shadow: 5px 5px 10px #e3e3ea, -5px -5px 10px #ffffff;
  background: #fbfbfd;
  padding: 10px 30px 30px 30px;
}
</style>
